<?php

/**
 * Created by PhpStorm.
 * User: Xzareth
 * Date: 29/05/14
 * Time: 10:22 PM
 */
class Order
{
    private $db;

    public function __construct(PDO $db)
    {
        $this->db = $db;
    }

    public function createOrder($userID, $billID, $shipID, $status, $total, $shipDate)
    {
        $sql = "INSERT INTO Orders ('UserID', 'BillingID', 'ShippingID', 'OrderDate', 'OrderStatus', 'TotalPrice', 'ShippingDate') VALUES (?, ?, ?, ?, ?, ?, ?)";
        $query = $this->db->prepare($sql);

        $query->bindValue(1, $userID);
        $query->bindValue(2, $billID);
        $query->bindValue(3, $shipID);
        $query->bindValue(4, date('Y-m-d'));
        $query->bindValue(5, $status);
        $query->bindValue(6, $total);
        $query->bindValue(7, $shipDate);

        try {
            $query->execute();

            if ($query) return "Order was successfully inserted into the database!";

        } catch (PDOException $e) {
            echo $e->getMessage();
        }

        return "Order was NOT inserted into the database.";
    }

    public function updateOrder($orderID, $status, $total, $shipDate)
    {
        $sql = "UPDATE Orders SET OrderStatus = ?, TotalPrice = ?, ShippingDate = ? WHERE OrderID = ?";
        $query = $this->db->prepare($sql);

        $query->bindValue(1, $status);
        $query->bindValue(2, $total);
        $query->bindValue(3, $shipDate);
        $query->bindValue(4, $orderID);

        try {
            $query->execute();
            if ($query) return "OrderID: $orderID was updated successfully!";

        } catch (PDOException $e) {
            echo $e->getMessage();
        }

        return "OrderID: $orderID was not updated successfully!";
    }

    public function deleteOrder($orderID)
    {
        $sql = "DELETE FROM Orders WHERE OrderID = ?";
        $query = $this->db->prepare($sql);

        $query->bindValue(1, $orderID);

        try {
            $query->execute();
            if ($query) return "Order was successfully deleted!";

        } catch (PDOException $e) {
            echo $e->getMessage();
        }
        return "Unable to delete Order!";
    }

} 